package cn.cai.pethome.base.controller;

import cn.cai.pethome.base.util.AjaxResult;
import cn.cai.pethome.logininfo.service.ILogininfoService;
import cn.cai.pethome.user.dto.UserDto;
import cn.cai.pethome.user.service.IUserService;
import org.assertj.core.error.ShouldAccept;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * 登录或者注册、退出登录的controller
 * 为何需要把登录、注册、退出登录需要单独搞一个Controller？？？
 *  我们后面马上要做登录，以及检查是否已登录的拦截器，登录注册以及退出登录都无需检查，需要放行
 *  到时候直接放行  /lr/**  即可
 */
@RestController
@RequestMapping("/lr")
public class LoginAndRegisterController {
    @Autowired
    private IUserService userService;

    @Autowired
    private ILogininfoService logininfoService;

    /**
     * 手机短信验证码注册
     * @param userDto
     * @return
     */
    @PostMapping("/phoneRegister")
    public AjaxResult phoneRegister(@RequestBody UserDto userDto){
        try {
            userService.phoneRegister(userDto);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }

    }

    /**
     * 用户账号密码登录需要实现的代码
     * @param userDto  登录需要的对象
     * @param           //登录成功 把当前登录用户信息保存在Session中
     * @return
     */
    @PostMapping("/accountLogin")
    public AjaxResult accountLogin(@RequestBody UserDto userDto){
        try {

            return logininfoService.accountLogin(userDto);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }


    }

    @Value("${wechat.appid}")
    private String appid;

    @Value("${wechat.redirectUrl}")
    private String redirectUrl;

    /**
     * 微信扫码登录，
     * @return
     */
    @GetMapping("/getWechatInfo")
    public AjaxResult getWechatInfo(){
        try {
            Map<String,String> map = new HashMap<>();
            map.put("appid",appid);  //前端调用引号里面的别名，"appid"
            map.put("redirectUrl",redirectUrl);
            return AjaxResult.success(map);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("微信登录失败！！！");

        }


    }

    /**
     * 通过code授权码换取access_token
     * @param code
     * @return
     */
    @GetMapping("/wechatLogin/{code}")
    public AjaxResult wechatLogin(@PathVariable("code")String code){

        try {
            return logininfoService.wechatLogin(code);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 微信用户绑定手机号
     * @param userDto
     * @return
     */
    @PostMapping("/wechatUserBind")
    public AjaxResult wechatUserBind(@RequestBody UserDto userDto){
        try {
            return logininfoService.wechatUserBind(userDto);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }


    /**
     * 用户短信验证码登录
     * @param userDto
     * @return
     */
    @PostMapping("/smsLogin")
    public AjaxResult smsLogin(@RequestBody UserDto userDto){
        try {
            return logininfoService.smsLogin(userDto);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 后台登录
     * @param userDto
     * @return
     */
    @PostMapping("/backstageLogin")
    public AjaxResult backstageLogin(@RequestBody UserDto userDto){
        try {
            return logininfoService.backstageLogin(userDto);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }
}
